/**
 * 
 */
package com.mycompany.client;

import com.google.gwt.user.client.History;
import com.mycompany.shared.EnvConstants;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

/**
 * @author coral.ma
 *
 */
public class PageFlowController {

	private static TabSet mainTabSet;
	
	public static void pageController(String pageID) {
		Tab tab = null;
		if (pageID != null) {
            String tabID = pageID + "_tab";
            tab = mainTabSet.getTab(tabID);
        }
		if (tab == null) {
			CoralPage page = CoralPageFactory.getPanel(pageID);
			if(page == null) {
				return;
			}
			tab = new Tab();
            tab.setID(pageID + "_tab");
            tab.setAttribute("historyToken", pageID);
            
            String pageName = page.getPageName();
            String icon = page.getIcon();
            if (icon == null) {
                icon = "silk/plugin.png";
            }
            String imgHTML = Canvas.imgHTML(icon, 16, 16);
            tab.setTitle("<span>" + imgHTML + "&nbsp;" + pageName + "&nbsp;&nbsp;</span>");
            tab.setPane(page.getViewPage());
            
            if(EnvConstants.SINGLE_PAGE) {
            	tab.setCanClose(false);
            	int index = mainTabSet.getTabs().length;
            	if(index > 0) {
            		mainTabSet.removeTab(0);
            	}
    		} else {
	            tab.setCanClose(true);
    		}
            mainTabSet.addTab(tab);
            mainTabSet.selectTab(tab);
		} else {
            mainTabSet.selectTab(tab);
		}
		History.newItem(pageID, false);
	}

	/**
	 * @return the mainTabSet
	 */
	public static TabSet getMainTabSet() {
		return mainTabSet;
	}

	/**
	 * @param mainTabSet the mainTabSet to set
	 */
	public static void setMainTabSet(TabSet mainTabSet) {
		PageFlowController.mainTabSet = mainTabSet;
	}
	
}
